package com.ruoyi.inducePest.controller.device;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.inducePest.domain.device.DeviceLureChangeRecord;
import com.ruoyi.inducePest.domain.device.DeviceTypeAndCount;
import com.ruoyi.inducePest.domain.device.InduceDeviceInfo;
import com.ruoyi.inducePest.domain.device.Lure;
import com.ruoyi.inducePest.mapper.device.InduceDeviceMapper;
import com.ruoyi.inducePest.service.device.InduceDeviceLureChangeRecordService;
import com.ruoyi.inducePest.service.device.InduceDeviceService;
import com.ruoyi.permission.Dao.SerBlockDevice;
import com.ruoyi.permission.Dao.SerDevicePermi;
import com.ruoyi.permission.Dao.SerUnitBase;
import com.ruoyi.permission.domain.BaseBlockPermiDeviceInfo;
import com.ruoyi.permission.domain.BlockPermiDeviceInfo;
import com.ruoyi.permission.mapper.*;
import com.ruoyi.permission.service.PermiService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @program: ruoyi_202130504106_李航
 * @description:
 * @author: LiHang
 * @create: 2022-04-11 09:53
 **/
@Api("设备相关")
@Slf4j
@RestController
@RequestMapping("/inducePest/device")
public class DeviceControll extends BaseController {

    @Autowired
    private InduceDeviceService induceDeviceService;

    @Autowired
    InduceDeviceMapper induceDeviceMapper;

    @Autowired
    SerDevicePermiMapper devicePermiMapper;

    @Autowired
    SerUnitUserMapper unitUserMapper;

    @Autowired
    SerUnitBaseMapper unitBaseMapper;

    @Autowired
    SerBaseBlockMapper baseBlockMapper;

    @Autowired
    SerBlockDeviceMapper blockDeviceMapper;

    @Autowired
    private PermiService permiService;

    @Autowired
    InduceDeviceLureChangeRecordService induceDeviceLureChangeRecordService;
    /**
     * 添加设备信息
     * @param induceDeviceInfo
     * @return
     */
//    @ApiOperation("根据条件查询列表，支持分页使用 numSize、numPage.根据时间查找，加上beginTime和end参数示例如下：" +
//            "http://localhost:8182/inducePest/device/insertDeviceInfo")
    @PostMapping("/insertDeviceInfo")
    public AjaxResult insertDeviceInfo(InduceDeviceInfo induceDeviceInfo) {
        int i = induceDeviceService.insertDeviceInfo(induceDeviceInfo);
        List<InduceDeviceInfo> induceDeviceInfos = induceDeviceMapper.selectDeviceInfoByCondition(induceDeviceInfo);
        InduceDeviceInfo induceDeviceInfo1 = induceDeviceInfos.get(0);
        SerDevicePermi serDevicePermi = new SerDevicePermi().setDeviceSerial(induceDeviceInfo.getCameraId()).setDeviceType("性诱捕器").setTypeId(2)
                .setDeviceNo(induceDeviceInfo1.getId()).setManualId(induceDeviceInfo.getManualId());
        devicePermiMapper.insert(serDevicePermi);
        if(i>0){
            return AjaxResult.success("添加设备信息成功，请刷新");
        }
        return AjaxResult.error("添加设备信息失败，请重试");
    }

    /**
     * 查询所有设备信息
     * @return
     */
    @ApiOperation("根据条件查询列表，支持分页使用 numSize、numPage.根据时间查找，加上beginTime和end参数示例如下：" +
            "http://localhost:8182/inducePest/device/selectAllDeviceInfo")
    @GetMapping("/selectAllDeviceInfo")
    public TableDataInfo selectAllDeviceInfo() {
        Long userIdL = SecurityUtils.getLoginUser().getUser().getUserId();
        Integer userId= userIdL.intValue();
        Integer unitId = 0;
        if(userId != 1 && userId != 20) {
            unitId = unitUserMapper.selectByPrimaryKey(userId).getUnitId();
        }
        //管理员用户
        if(unitId == 0) {
            return induceDeviceService.selectAllDeviceInfo();
        }else{
            List<BaseBlockPermiDeviceInfo> baseBlockPermiDeviceInfoList = permiService.selectAllDeviceByUnitId(unitId);
            List<SerDevicePermi> induceDevicePermiList = new ArrayList<>();
            for(BaseBlockPermiDeviceInfo baseBlockPermiDeviceInfo:baseBlockPermiDeviceInfoList) {
                if (baseBlockPermiDeviceInfo.getTypeId() == 2) {
                    SerDevicePermi serDevicePermi = new SerDevicePermi().setDeviceName(baseBlockPermiDeviceInfo.getDeviceName()).setDeviceNo(baseBlockPermiDeviceInfo.getDeviceId())
                            .setDeviceType(baseBlockPermiDeviceInfo.getDeviceType()).setDeviceSerial(baseBlockPermiDeviceInfo.getDeviceSerial()).setId(baseBlockPermiDeviceInfo.getId());
                    induceDevicePermiList.add(serDevicePermi);
                }
            }
            InduceDeviceInfo induceDeviceInfo = new InduceDeviceInfo();
            return induceDeviceService.selectDeviceInfoByCondition(induceDeviceInfo,induceDevicePermiList);
        }
    }

    /**
     * 根据条件查询设备信息
     * @param induceDeviceInfo
     * @return
     */
//    @ApiOperation("根据条件查询列表，支持分页使用 numSize、numPage.根据时间查找，加上beginTime和end参数示例如下：" +
//            "http://localhost:8182/inducePest/device/selectDeviceInfoByCondition")
    @PostMapping("/selectDeviceInfoByCondition")
    public TableDataInfo selectDeviceInfoByCondition(InduceDeviceInfo induceDeviceInfo) {
        Long userIdL = SecurityUtils.getLoginUser().getUser().getUserId();
        Integer userId=Integer.valueOf(userIdL.intValue());
        Integer unitId = 0;
        if(userId != 1 && userId != 20) {
            unitId = unitUserMapper.selectByPrimaryKey(userId).getUnitId();
        }

        //管理员用户
        if(unitId == 0) {
            List<SerDevicePermi> allDeviceInfos = new ArrayList<>();
            return induceDeviceService.selectDeviceInfoByCondition(induceDeviceInfo,allDeviceInfos);
        }else{
            List<BaseBlockPermiDeviceInfo> baseBlockPermiDeviceInfoList = permiService.selectAllDeviceByUnitId(unitId);
            List<SerDevicePermi> induceDevicePermiList = new ArrayList<>();
            for(BaseBlockPermiDeviceInfo baseBlockPermiDeviceInfo:baseBlockPermiDeviceInfoList) {
                if (baseBlockPermiDeviceInfo.getTypeId() == 2) {
                    SerDevicePermi serDevicePermi = new SerDevicePermi().setDeviceName(baseBlockPermiDeviceInfo.getDeviceName()).setDeviceNo(baseBlockPermiDeviceInfo.getDeviceId())
                            .setDeviceType(baseBlockPermiDeviceInfo.getDeviceType()).setDeviceSerial(baseBlockPermiDeviceInfo.getDeviceSerial()).setId(baseBlockPermiDeviceInfo.getId());
                    induceDevicePermiList.add(serDevicePermi);
                }
            }
            return induceDeviceService.selectDeviceInfoByCondition(induceDeviceInfo,induceDevicePermiList);
        }
    }

    /**
     * 根据id修改设备信息
     * @param induceDeviceInfo
     * @return
     */
//    @ApiOperation("根据id修改设备信息" +
//            "http://localhost:8182/inducePest/device/updateDeviceInfoById")
    @PostMapping("/updateDeviceInfoById")
    public AjaxResult updateDeviceInfoById(InduceDeviceInfo induceDeviceInfo) throws IOException {
        int i = induceDeviceService.updateDeviceInfoById(induceDeviceInfo);
        if(i>0){
            return AjaxResult.success("修改设备信息成功，请刷新");
        }
        return AjaxResult.error("修改设备信息失败，请重试");
    }

    /**
     * 查询设备类别及该类别的数量
     * @param
     * @return
     */
//    @ApiOperation("查询设备类别及该类别的数量" +
//            "http://localhost:8182/inducePest/device/selectDeviceTypeAndCount")
    @PostMapping("/selectDeviceTypeAndCount")
    public AjaxResult selectDeviceTypeAndCount() {
        List<DeviceTypeAndCount> deviceTypeAndCounts = induceDeviceService.selectDeviceTypeAndCount();

        return AjaxResult.success(deviceTypeAndCounts);
    }

    /**
     * 查询所有诱芯种类
     * @param
     * @return
     */
//    @ApiOperation("查询所有诱芯类别" +
//            "http://localhost:8182/inducePest/device/selectLure")
    @GetMapping("/selecAllLure")
    public AjaxResult selectAllLure() {
        List<String> alltLure = induceDeviceService.selecAlltLure();
        return AjaxResult.success(alltLure);
    }

    /**
     * 根据条件查询诱芯种类
     * @param
     * @return
     */
//    @ApiOperation("根据条件查询诱芯类别" +
//            "http://localhost:8182/inducePest/device/selectLure")
    @GetMapping("/selectLure")
    public AjaxResult selectLure(Lure lure) {
        List<Lure> alltLure = induceDeviceService.selectLure(lure);
        return AjaxResult.success(alltLure);
    }

    /**
     * 增加诱芯种类
     * @param
     * @return
     */
//    @ApiOperation("插入诱芯类别" +
//            "http://localhost:8182/inducePest/device/selectLure")
    @GetMapping("/insertLure")
    public AjaxResult insertLure(Lure lure) {
        int i=induceDeviceService.insertLure(lure);
        if(i>0) {
            return AjaxResult.success("插入成功");
        }else {
            return AjaxResult.error("插入失败");
        }
    }

    /**
     * 更新诱芯种类
     * @param
     * @return
     */
//    @ApiOperation("更新诱芯类别" +
//            "http://localhost:8182/inducePest/device/selectLure")
    @GetMapping("/updateLure")
    public AjaxResult updateLure(Lure lure) {
        int i=induceDeviceService.updateLure(lure);
        if(i>0) {
            return AjaxResult.success("修改成功");
        }else {
            return AjaxResult.error("修改失败");
        }
    }

    /**
     * 根据id删除诱芯类别
     * @param
     * @return
     */
//    @ApiOperation("根据id删除诱芯类别" +
//            "http://localhost:8182/inducePest/device/selectLure")
    @GetMapping("/deleteLure")
    public AjaxResult deleteLure(Lure lure) {
        int i=induceDeviceService.deleteLure(lure);
        if(i>0) {
            return AjaxResult.success("删除成功");
        }else {
            return AjaxResult.error("删除失败");
        }
    }


    /**
     * 根据条件查询诱芯更换记录
     * @param
     * @return
     */
    @ApiOperation("根据条件查询诱芯更换记录")
    @PostMapping("/selectLureChangeRecordByCondition")
    public TableDataInfo selectLureChangeRecordByCondition(DeviceLureChangeRecord deviceLureChangeRecord, Integer pageNum, Integer pageSize) {
        TableDataInfo tableDataInfo = induceDeviceLureChangeRecordService.selectInfoByCondition(deviceLureChangeRecord, pageNum, pageSize);

        return tableDataInfo;

    }




}
